package com.css.fxfzypg.modules.statistics.service.impl;


import com.css.fxfzypg.base.dict.repository.entity.DictItemEntity;
import com.css.fxfzypg.base.dict.service.DictService;
import com.css.fxfzypg.manage.building.repository.entity.BuildingEntity;
import com.css.fxfzypg.manage.lethalitydifference.vo.SysMenuVo;
import com.css.fxfzypg.modules.maps.service.SetEarthquakeMapService;
import com.css.fxfzypg.modules.maps.service.SlopeMapService;
import com.css.fxfzypg.modules.maps.service.TaskRegionMapService;
import com.css.fxfzypg.modules.statistics.repository.StatisticsRepository;

import com.css.fxfzypg.modules.statistics.service.StatisticsService;
import com.css.fxfzypg.modules.statistics.vo.QueryParams;
import com.css.fxfzypg.modules.statistics.vo.ResultVo;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.*;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    StatisticsRepository statisticsRepository;
    @Autowired
    SysAreaService sysAreaService;
    @Autowired
    TaskRegionMapService taskRegionMapService;
    @Autowired
    DictService dictService;
    @Autowired
    SetEarthquakeMapService setEarthquakeMapService;
    @Autowired
    SlopeMapService slopeMapService;

    @Override
    public ResultVo getNumberByType(QueryParams queryParams) {
        ResultVo result = new ResultVo();
        if(queryParams.getType().equals("3")){//坡度
            //以下代码已废弃（用于上传的坡度文件解析并存储到数据库坡度表数据进行分段面积统计）
            /*List<Double> list = new ArrayList<>();
            BigDecimal num = BigDecimal.ZERO;
            BigDecimal num2 = BigDecimal.ZERO;
            BigDecimal num3 = BigDecimal.ZERO;
            BigDecimal num4 = BigDecimal.ZERO;
            if(!PlatformObjectUtils.isEmpty(queryParams.getGeomText())) {//地图圈选
                List<ResultVo> resultVoList = statisticsRepository.getSlopeByGeomText(queryParams.getGeomText());
                for(ResultVo resultVo :resultVoList){
                    if(resultVo.getSlopeNumber()<=30){
                        num = num.add(resultVo.getStArea());
                        //num+=resultVo.getStArea();
                    }else if(resultVo.getSlopeNumber()>30 && resultVo.getSlopeNumber()<=45){
                        num2 = num2.add(resultVo.getStArea());
                       // num2+=resultVo.getStArea();
                    }else if(resultVo.getSlopeNumber()>45 && resultVo.getSlopeNumber()<=60){
                        num3 = num3.add(resultVo.getStArea());
                        //num3+=resultVo.getStArea();
                    }else if( resultVo.getSlopeNumber() > 60){
                        num4 = num4.add(resultVo.getStArea());
                        //num4+=resultVo.getStArea();
                    }
                }
            }else{
                if(!PlatformObjectUtils.isEmpty(queryParams.getFlag())){
                    String flag = queryParams.getFlag();
                    String divisionId = "";
                    if(flag.equals("0")){
                        divisionId = queryParams.getProvinceId();
                    }else if(flag.equals("1")){
                        divisionId = queryParams.getCityId();
                    }else if(flag.equals("2")){
                        divisionId = queryParams.getAreaId();
                    }
                    String divisionCode = sysAreaService.getDivisionCodeById(divisionId);
                    divisionCode = divisionCode.substring(1,divisionCode.length()-1);
                    queryParams.setDivisionCode(divisionCode);
                    List<ResultVo> resultVoList = statisticsRepository.getSlopeByDivision(queryParams);
                    for(ResultVo resultVo :resultVoList){
                        if(resultVo.getSlopeNumber()<=30){
                            num = num.add(resultVo.getStArea());
                        }else if(resultVo.getSlopeNumber()>30 && resultVo.getSlopeNumber()<=45){
                            num2 = num2.add(resultVo.getStArea());
                        }else if(resultVo.getSlopeNumber()>45 && resultVo.getSlopeNumber()<=60){
                            num3 = num3.add(resultVo.getStArea());
                        }else if( resultVo.getSlopeNumber() > 60){
                            num4 = num4.add(resultVo.getStArea());
                        }
                    }
                }else{
                    List<ResultVo> resultVoList = statisticsRepository.getSlopeAll();//全国
                    for(ResultVo resultVo :resultVoList){
                        if(resultVo.getSlopeNumber()<=30){
                            num = num.add(resultVo.getStArea());
                        }else if(resultVo.getSlopeNumber()>30 && resultVo.getSlopeNumber()<=45){
                            num2 = num2.add(resultVo.getStArea());
                        }else if(resultVo.getSlopeNumber()>45 && resultVo.getSlopeNumber()<=60){
                            num3 = num3.add(resultVo.getStArea());
                        }else if( resultVo.getSlopeNumber() > 60){
                            num4 = num4.add(resultVo.getStArea());
                        }
                    }
                }
            }
            list.add(div(num));
            list.add(div(num2));
            list.add(div(num3));
            list.add(div(num4));*/

            //新方法：查询上传的坡度文件，解析文件并统计面积
            //根据省市县查询tif文件栅格数据统计面积
            List<Double> list = slopeMapService.countAllTifSlopeArea(queryParams.getFlag(),queryParams.getProvince(),queryParams.getCity(),queryParams.getArea(),queryParams.getGeomText());

            result.setSlopeYList(list);

        }else if(queryParams.getType().equals("4")){//人口建筑物经济公里格网shp格式
            List<String> populationXList = new ArrayList<>();//人口横轴
            List<BigDecimal> populationYList = new ArrayList<>();//人口竖轴
            List<String> gdpXList = new ArrayList<>();//gdp横轴
            List<BigDecimal> gdpYList = new ArrayList<>();//gdp竖轴
            List<String> coveredareaXList = new ArrayList<>();//建筑面积横轴
            List<BigDecimal> coveredareaYList = new ArrayList<>();//建筑面积竖轴
            if(!PlatformObjectUtils.isEmpty(queryParams.getGeomText())){//地图圈选
                Map<String, List<ResultVo>> map = new HashMap<>();
                List<ResultVo> list;
                List<ResultVo> ResultVoList = statisticsRepository.getKilometreGridByGeomText(queryParams.getGeomText());
                for(ResultVo resultVo : ResultVoList){
                    if(queryParams.getFlag().equals("0")){
                        if(map.containsKey(resultVo.getProvince())){
                            list = map.get(resultVo.getProvince());
                        }else{
                            list = new ArrayList<>();
                        }
                        list.add(resultVo);
                        map.put(resultVo.getProvince(),list);
                    }else if(queryParams.getFlag().equals("1")){
                        String key = resultVo.getCity();
                        if(map.containsKey(key)){
                            list = map.get(key);
                        }else{
                            list = new ArrayList<>();
                        }
                        list.add(resultVo);
                        map.put(key,list);

                    }else if(queryParams.getFlag().equals("2")){
                        String key = resultVo.getArea();
                        if(map.containsKey(key)){
                            list = map.get(key);
                        }else{
                            list = new ArrayList<>();
                        }
                        list.add(resultVo);
                        map.put(key,list);
                    }
                }

                for (String key : map.keySet()) {
                    List<ResultVo> list1 = map.get(key);
                    for (ResultVo resultVo : list1) {
                        populationXList.add(key);
                        populationYList.add(resultVo.getPopulation().divide(BigDecimal.valueOf(10000),3,BigDecimal.ROUND_HALF_UP));
                        coveredareaXList.add(key);
                        coveredareaYList.add(resultVo.getCoveredarea().divide(BigDecimal.valueOf(1000000),3,BigDecimal.ROUND_HALF_UP));
                        gdpXList.add(key);
                        gdpYList.add(resultVo.getGdp().divide(BigDecimal.valueOf(10000),3,BigDecimal.ROUND_HALF_UP));
                    }
                }
            }else{//行政区划
                List<ResultVo> resultVoList = statisticsRepository.getKilometreGridByDivision(queryParams);
                BigDecimal populationTotal = BigDecimal.ZERO;
                BigDecimal coveredareaTotal = BigDecimal.ZERO;
                BigDecimal gdpTotal = BigDecimal.ZERO;
                Map<String, List<ResultVo>> map = new HashMap<>();
                List<ResultVo> list;
                for(ResultVo resultVo : resultVoList){
                    if(queryParams.getFlag().equals("0")){
                        if(map.containsKey(resultVo.getProvince())){
                            list = map.get(resultVo.getProvince());
                        }else{
                            list = new ArrayList<>();
                        }
                        list.add(resultVo);
                        map.put(resultVo.getProvince(),list);
                    }else if(queryParams.getFlag().equals("1")){
                        String key = resultVo.getCity();
                        if(map.containsKey(key)){
                            list = map.get(key);
                        }else{
                            list = new ArrayList<>();
                        }
                        list.add(resultVo);
                        map.put(key,list);

                    }else if(queryParams.getFlag().equals("2")){
                        String key = resultVo.getArea();
                        if(map.containsKey(key)){
                            list = map.get(key);
                        }else{
                            list = new ArrayList<>();
                        }
                        list.add(resultVo);
                        map.put(key,list);
                    }
                }
                for (String key : map.keySet()) {
                    List<ResultVo> list1 = map.get(key);
                    populationXList.add(key);
                    coveredareaXList.add(key);
                    gdpXList.add(key);
                    for (ResultVo resultVo : list1) {
                        populationTotal = populationTotal.add(resultVo.getPopulation());
                        coveredareaTotal = coveredareaTotal.add(resultVo.getCoveredarea());
                        gdpTotal = gdpTotal.add(resultVo.getGdp());
                    }
                    populationYList.add(populationTotal.divide(BigDecimal.valueOf(10000),3,BigDecimal.ROUND_HALF_UP));
                    coveredareaYList.add(coveredareaTotal.divide(BigDecimal.valueOf(1000000),3,BigDecimal.ROUND_HALF_UP));
                    gdpYList.add(gdpTotal.divide(BigDecimal.valueOf(10000),3,BigDecimal.ROUND_HALF_UP));

                    populationTotal=BigDecimal.ZERO;
                    coveredareaTotal=BigDecimal.ZERO;
                    gdpTotal=BigDecimal.ZERO;
                }
            }
            result.setPopulationXList(populationXList);
            result.setPopulationYList(populationYList);
            result.setGdpXList(gdpXList);
            result.setGdpYList(gdpYList);
            result.setCoveredareaXList(coveredareaXList);
            result.setCoveredareaYList(coveredareaYList);
        } else if(queryParams.getType().equals("6")){//机场
            int airport = statisticsRepository.getAirport(queryParams);
            result.setAirport(airport);
        }else if(queryParams.getType().equals("7")){//水库
            int reservoir = statisticsRepository.getReservoir(queryParams);
            result.setReservoir(reservoir);
        }else if(queryParams.getType().equals("8")){//学校
            List<String> x = new ArrayList<>();
            List<Integer> y = new ArrayList<>();
            x.add("小学");
            x.add("中学");
            x.add("大学");
            int count = statisticsRepository.getSchool("小学",queryParams);
            int count2 = statisticsRepository.getSchool("中学",queryParams);
            int count3 = statisticsRepository.getSchool("大学",queryParams);
            y.add(count);
            y.add(count2);
            y.add(count3);
            result.setSchoolXList(x);
            result.setSchoolYList(y);
        }else if(queryParams.getType().equals("9")){//医院
            List<Integer> hospitalNumList = new ArrayList<>();//医院数量
            int num = statisticsRepository.getHospitalNum("三级甲等",queryParams);
            int num2 = statisticsRepository.getHospitalNum("三级乙等",queryParams);
            int num3 = statisticsRepository.getHospitalNum("三级丙等",queryParams);
            hospitalNumList.add(num);
            hospitalNumList.add(num2);
            hospitalNumList.add(num3);
            List<Integer> hospitalPowerList = new ArrayList<>();
            int powerNum = statisticsRepository.getHospitalPower("三级甲等",queryParams);
            int powerNum2 = statisticsRepository.getHospitalPower("三级乙等",queryParams);
            int powerNum3 = statisticsRepository.getHospitalPower("三级丙等",queryParams);
            hospitalPowerList.add(powerNum);
            hospitalPowerList.add(powerNum2);
            hospitalPowerList.add(powerNum3);
            List<Integer> hospitalBedNumList = new ArrayList<>();
            int bedNum = statisticsRepository.getHospitalBedNum("三级甲等",queryParams);
            int bedNum2 = statisticsRepository.getHospitalBedNum("三级乙等",queryParams);
            int bedNum3 = statisticsRepository.getHospitalBedNum("三级丙等",queryParams);
            hospitalBedNumList.add(bedNum);
            hospitalBedNumList.add(bedNum2);
            hospitalBedNumList.add(bedNum3);
            result.setHospitalNumList(hospitalNumList);
            result.setHospitalPowerList(hospitalPowerList);
            result.setHospitalBedNumList(hospitalBedNumList);
        }else if(queryParams.getType().equals("10")){//地质灾害数据
            List<String> disastersXList = new ArrayList<>();
            List<Integer> disastersYList = new ArrayList<>();
            disastersXList.add("滑坡");
            disastersXList.add("不稳定斜坡");
            disastersXList.add("危岩");
            disastersXList.add("泥石流");
            int count = statisticsRepository.getDisastersNum("滑坡",queryParams);
            int count2 = statisticsRepository.getDisastersNum("不稳定斜坡",queryParams);
            int count3 = statisticsRepository.getDisastersNum("危岩",queryParams);
            int count4 = statisticsRepository.getDisastersNum("泥石流",queryParams);
            disastersYList.add(count);
            disastersYList.add(count2);
            disastersYList.add(count3);
            disastersYList.add(count4);
            result.setDisastersXList(disastersXList);
            result.setDisastersYList(disastersYList);
        }else if(queryParams.getType().equals("12")){//危化企业
            List<String> riskEnterpriseXList = new ArrayList<>();
            List<Integer> riskEnterpriseYList = new ArrayList<>();
            riskEnterpriseXList.add("一级");
            riskEnterpriseXList.add("二级");
            riskEnterpriseXList.add("三级");
            riskEnterpriseXList.add("四级");
            int count = statisticsRepository.getRiskEnterpriseNum("一级",queryParams);
            int count2 = statisticsRepository.getRiskEnterpriseNum("二级",queryParams);
            int count3 = statisticsRepository.getRiskEnterpriseNum("三级",queryParams);
            int count4 = statisticsRepository.getRiskEnterpriseNum("四级",queryParams);
            riskEnterpriseYList.add(count);
            riskEnterpriseYList.add(count2);
            riskEnterpriseYList.add(count3);
            riskEnterpriseYList.add(count4);
            result.setRiskEnterpriseXList(riskEnterpriseXList);
            result.setRiskEnterpriseYList(riskEnterpriseYList);
        }else if(queryParams.getType().equals("13")){//加油（加气）站
            List<String> gasStationXList = new ArrayList<>();
            List<Integer> gasStationYList = new ArrayList<>();
            gasStationXList.add("一级");
            gasStationXList.add("二级");
            gasStationXList.add("三级");
            gasStationXList.add("四级");
            int count = statisticsRepository.getGasStationNum("一级",queryParams);
            int count2 = statisticsRepository.getGasStationNum("二级",queryParams);
            int count3 = statisticsRepository.getGasStationNum("三级",queryParams);
            int count4 = statisticsRepository.getGasStationNum("四级",queryParams);
            gasStationYList.add(count);
            gasStationYList.add(count2);
            gasStationYList.add(count3);
            gasStationYList.add(count4);
            result.setGasStationXList(gasStationXList);
            result.setGasStationYList(gasStationYList);
        }else if(queryParams.getType().equals("14")){//应急避难场所
            List<String> emergencyshelterXList = new ArrayList<>();
            List<Integer> emergencyshelterYList = new ArrayList<>();
            emergencyshelterXList.add("短期");
            emergencyshelterXList.add("暂时");
            emergencyshelterXList.add("中长期");
            int sum = statisticsRepository.getEmergencyshelterByType("短期",queryParams);
            int sum2 = statisticsRepository.getEmergencyshelterByType("暂时",queryParams);
            int sum3 = statisticsRepository.getEmergencyshelterByType("中长期",queryParams);
            emergencyshelterYList.add(sum);
            emergencyshelterYList.add(sum2);
            emergencyshelterYList.add(sum3);
            int num = statisticsRepository.getPeopleTotalNumber(queryParams);
            result.setEmergencyshelterXList(emergencyshelterXList);
            result.setEmergencyshelterYList(emergencyshelterYList);
            result.setPeopleTotalNumber(num);
        }else if(queryParams.getType().equals("15")){//基于遥感的房屋抗震能力初步评估数据
            List<String> kangzhenXList = new ArrayList<>();
            List<Integer> kangzhenYList = new ArrayList<>();
            kangzhenXList.add("估计抗震能力达标");
            kangzhenXList.add("疑似抗震能力不足");
            kangzhenXList.add("疑似抗震能力严重不足");
            int num = statisticsRepository.getHouseNum("估计抗震能力达标",queryParams);
            int num2 = statisticsRepository.getHouseNum("疑似抗震能力不足",queryParams);
            int num3 = statisticsRepository.getHouseNum("疑似抗震能力严重不足",queryParams);
            kangzhenYList.add(num);
            kangzhenYList.add(num2);
            kangzhenYList.add(num3);
            result.setKangzhenXList(kangzhenXList);
            result.setKangzhenYList(kangzhenYList);
        }else if(queryParams.getType().equals("16")){//建筑物单体数据
            result = getBuilding(queryParams);
        }else if(queryParams.getType().equals("17")){//抽样调查点
            int num = statisticsRepository.getSamplingNum(queryParams);
            result.setSamplingNum(num);
        }else if(queryParams.getType().equals("18")){//灾害风险点信息
            List<String> disasterRiskXList = new ArrayList<>();
            List<Integer> disasterRiskYList = new ArrayList<>();
            disasterRiskXList.add("滑坡");
            disasterRiskXList.add("崩塌");
            disasterRiskXList.add("火灾");
            disasterRiskXList.add("道路拥堵/中断");
            disasterRiskXList.add("危险品泄露");
            disasterRiskXList.add("其他");
            int num = statisticsRepository.getDisasterRiskNum("hp",queryParams);
            int num2 = statisticsRepository.getDisasterRiskNum("bt",queryParams);
            int num3 = statisticsRepository.getDisasterRiskNum("hz",queryParams);
            int num4 = statisticsRepository.getDisasterRiskNum("dlyd",queryParams);
            int num5 = statisticsRepository.getDisasterRiskNum("wxpxl",queryParams);
            int num6 = statisticsRepository.getDisasterRiskNum("qt",queryParams);
            disasterRiskYList.add(num);
            disasterRiskYList.add(num2);
            disasterRiskYList.add(num3);
            disasterRiskYList.add(num4);
            disasterRiskYList.add(num5);
            disasterRiskYList.add(num6);
            result.setDisasterRiskXList(disasterRiskXList);
            result.setDisasterRiskYList(disasterRiskYList);
        }
        return result;
    }

    @Override
    public Integer countByCounty(String province, String city, String county) {
        return statisticsRepository.countByCounty(province, city, county);
    }

    @Override
    public Integer countByCity(String province, String city) {
        return statisticsRepository.countByCity(province, city);
    }

    @Override
    public Integer countByProvince(String province) {
        return statisticsRepository.countByProvince(province);
    }

    @Override
    public Integer countByProvinceAndYear(String provinceName, String taskYear) {
        return statisticsRepository.countByProvinceAndYear(provinceName, taskYear);
    }

    public ResultVo getBuilding(QueryParams queryParams){
        ResultVo result = new ResultVo();
        List<String> buildingXList = new ArrayList<>();
        List<Integer> buildingYList = new ArrayList<>();
        List<Double> buildingYList2 = new ArrayList<>();
        List<String> buildingGeomList = new ArrayList<>();
        //if(!PlatformObjectUtils.isEmpty(queryParams.getGeomText())){
            if(queryParams.getBuildingFlag().equals("0")){
                buildingXList.add("高层建筑");
                buildingXList.add("钢结构");
                buildingXList.add("多层钢筋混凝土结构");
                buildingXList.add("砖混结构");
                buildingXList.add("砖木结构");
                buildingXList.add("土结构");
                buildingXList.add("木结构");
                buildingXList.add("石结构");
                buildingXList.add("其他结构");
                int count = statisticsRepository.getBuildingNumByStructure("0",queryParams);
                int count1 = statisticsRepository.getBuildingNumByStructure("1",queryParams);
                int count2 = statisticsRepository.getBuildingNumByStructure("2",queryParams);
                int count3 = statisticsRepository.getBuildingNumByStructure("3",queryParams);
                int count4 = statisticsRepository.getBuildingNumByStructure("4",queryParams);
                int count5 = statisticsRepository.getBuildingNumByStructure("5",queryParams);
                int count6 = statisticsRepository.getBuildingNumByStructure("6",queryParams);
                int count7 = statisticsRepository.getBuildingNumByStructure("7",queryParams);
                int count8 = statisticsRepository.getBuildingNumByStructure("8",queryParams);
                buildingYList.add(count);
                buildingYList.add(count1);
                buildingYList.add(count2);
                buildingYList.add(count3);
                buildingYList.add(count4);
                buildingYList.add(count5);
                buildingYList.add(count6);
                buildingYList.add(count7);
                buildingYList.add(count8);
            }else if(queryParams.getBuildingFlag().equals("1")){
                List<DictItemEntity> kznlfsz = dictService.getValidDictItemsByDictCode("kznlfsz");
                Double value = 0.0;
                Double value2 = 0.0;
                Double value3 = 0.0;
                Double value4 = 0.0;
                Double value5 = 0.0;
                for(DictItemEntity dictItemEntity : kznlfsz){
                    String dictItemName = dictItemEntity.getDictItemName();
                    if(dictItemName.equals("好")){
                        value = Double.valueOf(dictItemEntity.getDictItemCode());
                    }else if(dictItemName.equals("较好")){
                        value2 = Double.valueOf(dictItemEntity.getDictItemCode());
                    }else if(dictItemName.equals("一般")){
                        value3 = Double.valueOf(dictItemEntity.getDictItemCode());
                    }else if(dictItemName.equals("较差")){
                        value4 = Double.valueOf(dictItemEntity.getDictItemCode());
                    }else if(dictItemName.equals("差")){
                        value5 = Double.valueOf(dictItemEntity.getDictItemCode());
                    }
                }
                List<BuildingEntity> buildingEntityList = statisticsRepository.getBuildingByGeomText(queryParams);
                Map<String, List<BuildingEntity>> map = new HashMap<>();
                List<BuildingEntity> list;
                for (BuildingEntity buildingEntity :buildingEntityList){
                    if(queryParams.getFlag().equals("0")){//省
                        if(map.containsKey(buildingEntity.getProvince())){
                            list=map.get(buildingEntity.getProvince());
                        }else {
                            list=new ArrayList<>();
                        }
                        list.add(buildingEntity);
                        map.put(buildingEntity.getProvince(),list);
                    }else if(queryParams.getFlag().equals("1")){//市
                        String key = buildingEntity.getProvince()+"-"+buildingEntity.getCity();
                        if(map.containsKey(key)){
                            list=map.get(key);
                        }else {
                            list=new ArrayList<>();
                        }
                        list.add(buildingEntity);
                        map.put(key,list);
                    }else if(queryParams.getFlag().equals("2")){//区
                        String key = buildingEntity.getProvince()+"-"+buildingEntity.getCity()+"-"+buildingEntity.getCounty();
                        if(map.containsKey(key)){
                            list=map.get(key);
                        }else {
                            list=new ArrayList<>();
                        }
                        list.add(buildingEntity);
                        map.put(key,list);
                    }
                }

                for (String key : map.keySet()){
                    List<BuildingEntity> list1 = map.get(key);
                    Double num=0.0;
                    for(BuildingEntity buildingEntity : list1){
                        String seismicCapacity = buildingEntity.getSeismicCapacity();
                        if(seismicCapacity.equals("好")){
                            num+=value;
                        }else if(seismicCapacity.equals("较好")){
                            num+=value2;
                        }else if(seismicCapacity.equals("一般")){
                            num+=value3;
                        }else if(seismicCapacity.equals("较差")){
                            num+=value4;
                        }else if(seismicCapacity.equals("差")){
                            num+=value5;
                        }
                    }
                    buildingXList.add(key);
                    buildingYList2.add(division(num,list1.size()));
                    String geom = "";
                    if(queryParams.getFlag().equals("0")){
                        geom = statisticsRepository.getGeomByProvince(key);
                    } else if(queryParams.getFlag().equals("1")){
                        if(key.contains("北京市")||key.contains("重庆市")||key.contains("天津市")||key.contains("上海市")){
                            key = key.substring(0,3);
                            geom = statisticsRepository.getGeomByProvince(key);
                        }else{
                            String provinceName = key.split("-")[0];
                            String cityName = key.split("-")[1];
                            geom = statisticsRepository.getGeomByCity(provinceName,cityName);
                        }
                    } else if(queryParams.getFlag().equals("2")){
                        String provinceName = key.split("-")[0];
                        String cityName = key.split("-")[1];
                        String areaName = key.split("-")[2];
                        geom = statisticsRepository.getGeomByArea(provinceName,cityName,areaName);
                    }
                    buildingGeomList.add(geom);
                }
            }
            result.setBuildingXList(buildingXList);
            result.setBuildingYList2(buildingYList2);
            result.setBuildingYList(buildingYList);
            result.setBuildingGeom(buildingGeomList);
            return result;
    }

    private List<SysMenuVo> getSAreaTree(String divisionId) throws UnsupportedEncodingException {
        String jsonStr = sysAreaService.jsonSAreaTree(URLEncoder.encode(divisionId, "UTF-8"));
        Gson gson = CreateGson.createGson();
        Type srItemsStandardType = new TypeToken<List<SysMenuVo>>() {
        }.getType();
        List<SysMenuVo> sysMenuList = gson.fromJson(jsonStr, srItemsStandardType);
        return sysMenuList;
    }


    public Double division(Double a, int b){
        if(a ==0.0 || b==0){
            return 0.0;
        }
        double rate = new BigDecimal( a / b).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
        return rate;
    }

    public static double div(BigDecimal a1) {
        if(a1.toString().equals(null) || a1.toString().equals("0") ){
            return 0.0;
        }
        BigDecimal a2 = new BigDecimal(String.valueOf(a1));
        BigDecimal b2 = new BigDecimal("1000000");
        return a2.divide(b2, 3, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

}
